package basic.study.leetcode;

/**
 * @ClassName Mid48
 * @Description 旋转图像
 * @Company inspur
 * @Author Kevin
 * @Date 2020/6/22 10:44
 * @Version 1.0
 */
public class Mid48 {
    class Solution {
        public void rotate(int[][] matrix) {
            if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
                return;
            }
            int temp;
            //先转置
            for (int i = 0; i < matrix.length; i++) {
                for (int j = i; j < matrix[0].length; j++) {
                    temp = matrix[i][j];
                    matrix[i][j] = matrix[j][i];
                    matrix[j][i] = temp;
                }
            }
            //再左右互换
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix.length / 2; j++) {
                    temp = matrix[i][j];
                    matrix[i][j] = matrix[i][matrix.length - 1 - j];
                    matrix[i][matrix.length - 1 - j] = temp;
                }
            }
        }
    }

    class Solution1 {
        public void rotate(int[][] matrix) {
            int n = matrix.length;
            for (int i = 0; i < n / 2 + n % 2; i++) {
                for (int j = 0; j < n /2; j++) {
                    int[] temp = new int[4];
                    int row = i;
                    int col = j;
                    for (int k = 0; k < 4; k++) {
                        temp[k] = matrix[row][col];
                        int x = row;
                        row = col;
                        col = n - 1 - x;
                    }
                    for (int k = 0; k < 4; k++) {
                        matrix[row][col] = temp[(k+3) % 4];
                        int x = row;
                        row = col;
                        col = n - 1 - x;
                    }
                }
            }

        }
    }

//    作者：LeetCode
//    链接：https://leetcode-cn.com/problems/rotate-image/solution/xuan-zhuan-tu-xiang-by-leetcode/
//    来源：力扣（LeetCode）
//    著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
}
